package com.quick.android.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * pageAdapter的基本封装，适用于显示Banner图片
 * 
 * @author quick
 * 
 * @param <T>
 */
public abstract class BasePageAdapter<T> extends PagerAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<T> data;

	public BasePageAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}

	/** 设置初始数据 */
	public void setData(ArrayList<T> list) {
		data = list;
		notifyDataSetChanged();
	}

	/** 添加数据 */
	public void addData(ArrayList<T> list) {
		if (data == null) {
			data = list;
		} else {
			data.addAll(list);
		}
		notifyDataSetChanged();
	}

	public T getItem(int position) {
		return data == null ? null : data.get(position);
	}

	@Override
	public int getCount() {
		return data == null ? 0 : data.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == obj;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		T item = getItem(position);
		View convertView = initConvertView(mInflater, item);
		container.addView(convertView);
		return convertView;
	}

	/** 加载布局，添加事件，设置图片展示等 */
	public abstract View initConvertView(LayoutInflater mInflater, T item);

}
